package com.mbyte.easy.admin.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mbyte.easy.admin.entity.Equapply;
import com.mbyte.easy.admin.entity.Equipment;
import com.mbyte.easy.admin.service.IEquapplyService;
import com.mbyte.easy.admin.service.IEquipmentService;
import com.mbyte.easy.maintainapply.entity.Maintainapply;
import com.mbyte.easy.maintainapply.service.IMaintainapplyService;
import com.mbyte.easy.common.controller.BaseController;
import com.mbyte.easy.common.web.AjaxResult;
import com.mbyte.easy.security.entity.SysUser;
import com.mbyte.easy.security.mapper.SysUserMapper;
import com.mbyte.easy.util.PageInfo;
import com.mbyte.easy.util.Utility;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.util.ObjectUtils;
import java.util.List;
import java.time.LocalDate;
import java.time.LocalDateTime;

/**
* <p>
* 前端控制器
* </p>
* @author lp
* @since 2019-03-11
*/
@Controller
@RequestMapping("/maintainapply/maintainapply")
public class MaintainapplyController extends BaseController  {

    private String prefix = "maintainapply/maintainapply/";

    @Autowired
    private IMaintainapplyService maintainapplyService;
    @Autowired
    SysUserMapper sysUserMapper;
    @Autowired
    private IEquapplyService equapplyService;
    @Autowired
    private IEquipmentService equipmentService;

    /**
    * 查询列表
    *
    * @param model
    * @param pageNo
    * @param pageSize
    * @param maintainapply
    * @return
    */
    @RequestMapping
    public String index(Model model,@RequestParam(value = "pageNo", required = false, defaultValue = "1") Integer pageNo,@RequestParam(value = "pageSize", required = false, defaultValue = "20") Integer pageSize, String equCreatetimeSpace, Maintainapply maintainapply) {
        Page<Maintainapply> page = new Page<Maintainapply>(pageNo, pageSize);
        QueryWrapper<Maintainapply> queryWrapper = new QueryWrapper<Maintainapply>();
        if(!ObjectUtils.isEmpty(maintainapply.getEquId())) {
            queryWrapper = queryWrapper.like("equ_id",maintainapply.getEquId());
         }
        if(!ObjectUtils.isEmpty(maintainapply.getEquName())) {
            queryWrapper = queryWrapper.like("equ_name",maintainapply.getEquName());
         }
        if(!ObjectUtils.isEmpty(maintainapply.getEquType())) {
            queryWrapper = queryWrapper.like("equ_type",maintainapply.getEquType());
         }
        if(!ObjectUtils.isEmpty(maintainapply.getPorposerId())) {
            queryWrapper = queryWrapper.like("porposer_Id",maintainapply.getPorposerId());
         }
        if(!ObjectUtils.isEmpty(maintainapply.getEquCreatetime())) {
            queryWrapper = queryWrapper.like("equ_createtime",maintainapply.getEquCreatetime());
         }
        if(!ObjectUtils.isEmpty(maintainapply.getStatus())) {
            queryWrapper = queryWrapper.like("status",maintainapply.getStatus());
         }
        if(!ObjectUtils.isEmpty(maintainapply.getFaultCause())) {
            queryWrapper = queryWrapper.like("fault_cause",maintainapply.getFaultCause());
         }
        if(!ObjectUtils.isEmpty(maintainapply.getMaintainRemake())) {
            queryWrapper = queryWrapper.like("maintain_remake",maintainapply.getMaintainRemake());
         }
        IPage<Maintainapply> pageInfo = maintainapplyService.page(page, queryWrapper);
        model.addAttribute("equCreatetimeSpace", equCreatetimeSpace);
        model.addAttribute("searchInfo", maintainapply);
        model.addAttribute("pageInfo", new PageInfo(pageInfo));
        return prefix+"list";
    }

    /**
    * 添加跳转页面
    * @return
    */
    @GetMapping("addBefore")
    public String addBefore(){
        return prefix+"add";
    }
    /**
    * 添加
    * @param maintainapply
    * @return
    */
    @PostMapping("add")
    @ResponseBody
    public AjaxResult add(Maintainapply maintainapply){
        return toAjax(maintainapplyService.save(maintainapply));
    }
    /**
    * 添加跳转页面
    * @return
    */
    @GetMapping("editBefore/{id}")
    public String editBefore(Model model,@PathVariable("id")Long id){
        model.addAttribute("maintainapply",maintainapplyService.getById(id));
        return prefix+"edit";
    }
    /**
    * 添加
    * @param maintainapply
    * @return
    */
    @PostMapping("edit")
    @ResponseBody
    public AjaxResult edit(Maintainapply maintainapply){
        return toAjax(maintainapplyService.updateById(maintainapply));
    }
    /**
    * 删除
    * @param id
    * @return
    */
    @GetMapping("delete/{id}")
    @ResponseBody
    public AjaxResult delete(@PathVariable("id") Long id){
        return toAjax(maintainapplyService.removeById(id));
    }
    /**
    * 批量删除
    * @param ids
    * @return
    */
    @PostMapping("deleteAll")
    @ResponseBody
    public AjaxResult deleteAll(@RequestBody List<Long> ids){
        return toAjax(maintainapplyService.removeByIds(ids));
    }


    /**
     * 个人用户  设备报修
     */

    @RequestMapping("personList")
    public String personList(Model model,@RequestParam(value = "pageNo", required = false, defaultValue = "1") Integer pageNo,@RequestParam(value = "pageSize", required = false, defaultValue = "20") Integer pageSize, Equipment equipment) {
        Page<Equipment> page = new Page<Equipment>(pageNo, pageSize);
        QueryWrapper<Equipment> queryWrapper = new QueryWrapper<Equipment>();
        if(!ObjectUtils.isEmpty(equipment.getEquId())) {
            queryWrapper = queryWrapper.like("equ_id",equipment.getEquId());
        }
        if(!ObjectUtils.isEmpty(equipment.getEquName())) {
            queryWrapper = queryWrapper.like("equ_name",equipment.getEquName());
        }
        if(!ObjectUtils.isEmpty(equipment.getEquType())) {
            queryWrapper = queryWrapper.like("equ_type",equipment.getEquType());
        }
        if(!ObjectUtils.isEmpty(equipment.getEquSizes())) {
            queryWrapper = queryWrapper.like("equ_sizes",equipment.getEquSizes());
        }
        if(!ObjectUtils.isEmpty(equipment.getEquStatus())) {
            queryWrapper = queryWrapper.like("equ_status",equipment.getEquStatus());
        }
        if(!ObjectUtils.isEmpty(equipment.getEquCreatetime())) {
            queryWrapper = queryWrapper.like("equ_createtime",equipment.getEquCreatetime());
        }
        if(!ObjectUtils.isEmpty(equipment.getEquSave())) {
            queryWrapper = queryWrapper.like("equ_save",equipment.getEquSave());
        }
        if(!ObjectUtils.isEmpty(equipment.getEquSupplier())) {
            queryWrapper = queryWrapper.like("equ_supplier",equipment.getEquSupplier());
        }
        if(!ObjectUtils.isEmpty(equipment.getEquRemaker())) {
            queryWrapper = queryWrapper.like("equ_remaker",equipment.getEquRemaker());
        }

        queryWrapper.eq("equ_status", "待维修");
        IPage<Equipment> pageInfo = equipmentService.page(page, queryWrapper);
        model.addAttribute("searchInfo", equipment);
        model.addAttribute("pageInfo", new PageInfo(pageInfo));
        return prefix+"person/personlist";
    }

    /**
     * 添加跳转页面
     * @return
     */
    @GetMapping("personaddBefore")
    public String personaddBefore(){
        return prefix+"person/personadd";
    }
    /**
     * 添加
     * @param maintainapply
     * @return
     */
    @PostMapping("personadd")
    @ResponseBody
    public AjaxResult personadd(Maintainapply maintainapply){
        return toAjax(maintainapplyService.save(maintainapply));
    }

    /**
     * 维修人员处理维修申请
     * @param model
     * @param pageNo
     * @param pageSize
     * @param equCreatetimeSpace
     * @param maintainapply
     * @return
     */
    @RequestMapping("dispose")
    public String dispose(Model model,@RequestParam(value = "pageNo", required = false, defaultValue = "1") Integer pageNo,@RequestParam(value = "pageSize", required = false, defaultValue = "20") Integer pageSize, String equCreatetimeSpace, Maintainapply maintainapply) {
        Page<Maintainapply> page = new Page<Maintainapply>(pageNo, pageSize);
        QueryWrapper<Maintainapply> queryWrapper = new QueryWrapper<Maintainapply>();
        if(!ObjectUtils.isEmpty(maintainapply.getEquId())) {
            queryWrapper = queryWrapper.like("equ_id",maintainapply.getEquId());
        }
        if(!ObjectUtils.isEmpty(maintainapply.getEquName())) {
            queryWrapper = queryWrapper.like("equ_name",maintainapply.getEquName());
        }
        if(!ObjectUtils.isEmpty(maintainapply.getEquType())) {
            queryWrapper = queryWrapper.like("equ_type",maintainapply.getEquType());
        }
        if(!ObjectUtils.isEmpty(maintainapply.getPorposerId())) {
            queryWrapper = queryWrapper.like("porposer_Id",maintainapply.getPorposerId());
        }
        if(!ObjectUtils.isEmpty(maintainapply.getEquCreatetime())) {
            queryWrapper = queryWrapper.like("equ_createtime",maintainapply.getEquCreatetime());
        }
        if(!ObjectUtils.isEmpty(maintainapply.getStatus())) {
            queryWrapper = queryWrapper.like("status",maintainapply.getStatus());
        }
        if(!ObjectUtils.isEmpty(maintainapply.getFaultCause())) {
            queryWrapper = queryWrapper.like("fault_cause",maintainapply.getFaultCause());
        }
        if(!ObjectUtils.isEmpty(maintainapply.getMaintainRemake())) {
            queryWrapper = queryWrapper.like("maintain_remake",maintainapply.getMaintainRemake());
        }
        IPage<Maintainapply> pageInfo = maintainapplyService.page(page, queryWrapper);
        model.addAttribute("equCreatetimeSpace", equCreatetimeSpace);
        model.addAttribute("searchInfo", maintainapply);
        model.addAttribute("pageInfo", new PageInfo(pageInfo));
        return prefix+"maintain/list";
    }

    /**
     * 添加跳转页面
     * @return
     */
    @GetMapping("disposeBefore/{id}")
    public String disposeBefore(Model model,@PathVariable("id")Long id){
        model.addAttribute("maintainapply",maintainapplyService.getById(id));
        return prefix+"maintain/dispose";
    }
    /**
     * 添加
     * @param maintainapply
     * @return
     */
    @PostMapping("dispose")
    @ResponseBody
    public AjaxResult dispose(Maintainapply maintainapply){
        return toAjax(maintainapplyService.updateById(maintainapply));
    }

    /**
     * 查询列表
     *
     * @param model
     * @param pageNo
     * @param pageSize
     * @param maintainapply
     * @return
     */
    @RequestMapping("applys")
    public String applys(Model model,@RequestParam(value = "pageNo", required = false, defaultValue = "1") Integer pageNo,@RequestParam(value = "pageSize", required = false, defaultValue = "20") Integer pageSize, String equCreatetimeSpace, Maintainapply maintainapply) {
        Page<Maintainapply> page = new Page<Maintainapply>(pageNo, pageSize);
        QueryWrapper<Maintainapply> queryWrapper = new QueryWrapper<Maintainapply>();
        if(!ObjectUtils.isEmpty(maintainapply.getEquId())) {
            queryWrapper = queryWrapper.like("equ_id",maintainapply.getEquId());
        }
        if(!ObjectUtils.isEmpty(maintainapply.getEquName())) {
            queryWrapper = queryWrapper.like("equ_name",maintainapply.getEquName());
        }
        if(!ObjectUtils.isEmpty(maintainapply.getEquType())) {
            queryWrapper = queryWrapper.like("equ_type",maintainapply.getEquType());
        }
        if(!ObjectUtils.isEmpty(maintainapply.getPorposerId())) {
            queryWrapper = queryWrapper.like("porposer_Id",maintainapply.getPorposerId());
        }
        if(!ObjectUtils.isEmpty(maintainapply.getEquCreatetime())) {
            queryWrapper = queryWrapper.like("equ_createtime",maintainapply.getEquCreatetime());
        }
        if(!ObjectUtils.isEmpty(maintainapply.getStatus())) {
            queryWrapper = queryWrapper.like("status",maintainapply.getStatus());
        }
        if(!ObjectUtils.isEmpty(maintainapply.getFaultCause())) {
            queryWrapper = queryWrapper.like("fault_cause",maintainapply.getFaultCause());
        }
        if(!ObjectUtils.isEmpty(maintainapply.getMaintainRemake())) {
            queryWrapper = queryWrapper.like("maintain_remake",maintainapply.getMaintainRemake());
        }
        IPage<Maintainapply> pageInfo = maintainapplyService.page(page, queryWrapper);
        model.addAttribute("equCreatetimeSpace", equCreatetimeSpace);
        model.addAttribute("searchInfo", maintainapply);
        model.addAttribute("pageInfo", new PageInfo(pageInfo));
        return prefix+"applys/applys";
    }


}

